﻿using MvcDemo.Filter;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace MvcDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        //[CustomActionFilter]
        //[CustomResultFilter]
        //[CustomAuthorizeFilter]
        //[CustomExceptionFilter]
        public ActionResult Index()
        {
            //int a = 0;
            //int b = 1 / a; 

            return View("Index1");
        }

        public ActionResult Index1()
        {
            dynamic x = new ExpandoObject();
            x.Id = "1";
            x.Name = "不详";
            x.Introduce = "无";

            ViewData.Model = x;
            return View();
        }

        public ActionResult Create()
        {
            if (Request.HttpMethod == "GET")
            {
                if (Request.QueryString.Keys.Count < 2)
                {
                    return View();
                }
                string text11 = Request.QueryString["Text11"];
                string text22 = Request.QueryString["Text22"];
                ViewData.Add("text", $"Text11={text11},Text22={text22}");
            }

            if (Request.HttpMethod == "POST")
            {
                string text11 = Request.Form["Text11"];
                string text22 = Request.Form["Text22"];
                ViewData.Add("text", $"Text11={text11},Text22={text22}");
            }
            return View();
        }



        public ActionResult EmptyResultDemo()
        {
            return new EmptyResult();
        }

        public ActionResult ContontResultDemo()
        {
            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("<!DOCTYPE html>");
            strHtml.Append("<html>");
            strHtml.Append("<head>");
            strHtml.Append("<body>");
            strHtml.Append("<p><h1>hello world</h1></p>");
            strHtml.Append("</body>");
            strHtml.Append("</head>");
            strHtml.Append("</html>");

            return Content(strHtml.ToString());
        }


        public ActionResult FileResultDemo()
        {
            return File(Server.MapPath("~/App_Data/2011012713070489.png"), "image/png");
        }

        public ActionResult JavaScriptDemo()
        {
            return JavaScript("alert('ok')");
        }

        public ActionResult JsonDemo()
        {
            return Json(new { id = 1, name = "Will", creadeton = DateTime.Now }, JsonRequestBehavior.AllowGet);
        }


        #region 异步测试 

        public async Task<ActionResult> AwaitDemo()
        {
            string html = await GetResponseHtmlAsync("http://www.cnblogs.com/");

            //Thread.Sleep(5000);

            return Content(html);

        }
        private async Task<string> GetResponseHtmlAsync(string url)
        {
            return await GetResponseContentAsync(url);
        }

        private async Task<string> GetResponseContentAsync(string url)
        {
            var httpClient = new System.Net.Http.HttpClient();

            var response = await httpClient.GetAsync(url);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                return "error";
            }
        }
        #endregion

    }
}